package com.itheima.health.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.service.CleanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Wu Ji  on 2020/12/6 15:09
 */
@Component
@Slf4j
public class BackUpCleanOrder {
    @Reference
    private CleanService cleanService;

    //@Scheduled(cron = "0 0 2 L * ? *")  //实战用
    //测试用
    @Scheduled(initialDelay = 3000,fixedDelay = 180000)
    public void backupAndCleanOrder() throws IOException {
/*        ClassPathResource res = new ClassPathResource("/template/orderset.xlsx");
        String path = res.getPath();
        System.out.println(path);
        System.out.println(res.getFile());*/


        String firstDay,lastDay;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获取前月的第一天
        Calendar cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);//此时的cal_1是上个月份
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置上个月的起始日期是1号
        firstDay = format.format(cal_1.getTime());//转变成上个月的1号
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH,1);//设置当前日期本月第一天
        cale.add(Calendar.DAY_OF_MONTH,-1);//此时日历是上个月的最后一天
        lastDay = format.format(cale.getTime());

        //测试写死
        deleteDate("2020-04-01","20-04-10");
        //实战需要打开
        //deleteDate(firstDay,lastDay)
        log.info("执行完了定时清理任务");
       /* try {
            Workbook wk = new XSSFWorkbook(res.getFile());
            //获取工作表
            Sheet sht = wk.getSheetAt(0);
            //数据库查询
            List<Map> dateList = cleanService.findOrderInfoBydate(firstDay,lastDay);
            int rowIndex = 1;
            for (Map map : dateList) {
                Row row = sht.getRow(rowIndex);
                row.getCell(0).setCellValue(((String) map.get("orderDate")));
                row.getCell(1).setCellValue((Integer)map.get("number"));
                row.getCell(2).setCellValue(((String) map.get("reservations")));
                row.getCell(3).setCellValue(((String) map.get("orderType")));
                row.getCell(4).setCellValue(((String) map.get("mealName")));
                row.getCell(5).setCellValue(((String) map.get("name")));
                row.getCell(6).setCellValue(((String) map.get("phoneNumber")));
                row.getCell(7).setCellValue((String) map.get("orderStatus"));
                rowIndex++;
            }
            String str = lastDay.substring(6);
            wk.write(new FileOutputStream(new File("E:\\"+str+".xlsx")));
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }*/


    }

    public void deleteDate(String fifirstDay,String lastDay){
        cleanService.deleteDate(fifirstDay,lastDay);
    }



}
